package com.example.LibrarySystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.LibrarySystem.Exception.ServiceException;
import com.example.LibrarySystem.mapper.BookMapper;
import com.example.LibrarySystem.mapper.BorrowingMapper;
import com.example.LibrarySystem.mapper.UserMapper;
import com.example.LibrarySystem.pojo.Book;
import com.example.LibrarySystem.pojo.Borrowing;
import com.example.LibrarySystem.pojo.dto.BorrowingDto;
import com.example.LibrarySystem.response.Result;
import com.example.LibrarySystem.service.BorrowingService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BorrowingServiceImpl implements BorrowingService {

    @Autowired
    private BorrowingMapper borrowingMapper;

    @Autowired
    private BookMapper bookMapper;

    /**
     * 条件查询
     *
     * @param data
     * @return
     */
    @Override
    public Result getBorrowByInfo(Map<String, String> data) {
        try {
            int start = data.containsKey("start") ? Integer.parseInt(data.get("start")) : 1;
            int pages = data.containsKey("pages") ? Integer.parseInt(data.get("pages")) : 10;

            String bookName = data.getOrDefault("bookName", "");
            String userName = data.getOrDefault("userName", "");
            String userSno = data.getOrDefault("userSno", "");

            // 分页查询数据
            List<Map<String, Object>> borrowByInfo = borrowingMapper.getBorrowByInfoPages(bookName, userName, userSno, (start - 1) * pages, pages);
            // 查询总条数
            List<Map<String, Object>> borrowByInfoNoPages = borrowingMapper.getBorrowByInfoNoPages(bookName, userName, userSno);
            Result result = new Result();
            result.setMsg("查询成功");
            result.setStatus(200);
            result.put("data", borrowByInfo);
            result.put("total",borrowByInfoNoPages.size());
            return result;
        } catch (Exception e) {
            throw new ServiceException("-1", "查询失败");
        }
    }

    @Override
    public List<Map<String, Object>> week() {
        List<Map<String, Object>> resList = new ArrayList<>();
        try {
            List<Map<String, Object>> weeks = borrowingMapper.week();
//        System.out.println(weeks);
            for (Map<String, Object> week : weeks) {
                Map<String, Object> map = new HashMap<>();

                Book book = bookMapper.selectById((int) week.get("book_id"));
                map.put("bookName", book.getName());
                map.put("version", book.getVersion());
                map.put("number", week.get("number"));

                resList.add(map);
            }
//        System.out.println(resList);
            return resList;
        } catch (Exception e) {
            throw new ServiceException("-1");
        }
    }

    @Override
    public List<Map<String, Object>> month() {
        List<Map<String, Object>> resList = new ArrayList<>();
        try {
            List<Map<String, Object>> months = borrowingMapper.month();
//        System.out.println(months);
            for (Map<String, Object> month : months) {
                Map<String, Object> map = new HashMap<>();

                Book book = bookMapper.selectById((int) month.get("book_id"));
                map.put("bookName", book.getName());
                map.put("version", book.getVersion());
                map.put("number", month.get("number"));
                resList.add(map);
            }
//        System.out.println(resList);
            return resList;
        } catch (Exception e) {
            throw new ServiceException("-1");
        }
    }
}
